﻿<%@ Page Title="Áreas de Conhecimento" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="AreaConhecimento.aspx.cs" Inherits="RzCorportativo.Views.Administrativo.AreaConhecimento" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="LeftContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <div class="tabbable tabs-left">
        <div class="tab-content">
            <div class="tab-pane active" id="geral">
                <div class="span12 well">
                    <div class="controls controls-row">
                        <asp:TextBox runat="server" ID="Nome" placeholder="Área" type="text" data-toggle="tooltip" title="Área" data-placement="left" has-dependent="true" target="AreaId"/>
                        <input type="button" name="btnNovo" value="salvar" id="btnAdionaArea" class="btn btn-success" />
                        <asp:HiddenField runat="server" ID="AreaId" />
                    </div>
                </div>
                <br />
                <div dataspy="">
                    <table class="table table-hover" id="gridArea" runat="server">
                        <thead>
                            <tr>
                                <th>
                                    Área
                                </th>
                                <th>
                                    #
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
                <script type="text/javascript">
                    $(function () {
                        $("input[has-dependent='true']").keyup(function () {
                            if ($(this).val().length == 0) {
                                $("input[id$='" + $(this).attr("target") + "']").val("");
                            }
                        });
                        $("input[id$='btnAdionaArea']").click(function (e) {
                            var area = new Area();
                            var alerta = new Alert();
                            alerta.Name = "erroGeral";
                            e.preventDefault();
                            area.Nome = $("input[id$='Nome']").val();
                            var id = $("input[id$='AreaId']").val();
                            area.AreaId = parseInt(id, 10);
                            if (id.length > 0 && isNaN(area.AreaId)) {
                                alerta.AddMessage("Código de alerta inválido");
                                area.AreaId = -1;
                            } else {
                                area.AreaId = isNaN(area.AreaId) ? 0 : area.AreaId;
                            }
                            if (area.Nome.length == 0) {
                                alerta.AddMessage("Código de alerta inválido");
                            }
                            if (alerta.HasMessage()) {
                                alerta.Type = AlertType.Error;
                                alerta.Show();
                                return false;
                            }
                            area.Salvar();
                            return false;
                        });
                    });
                    function Area() {
                        this.AreaId = 0;
                        this.Nome = "";
                        this.Ativo = true;
                        this.Salvar = function () {
                            var json = JSON.stringify(this);
                            current = this;
                            __post("Views/Administrativo/AreaConhecimento.aspx/AddArea", "{ area :" + json + " }", function (item) {
                                if ($("tr[area='" + current.AreaId + "']").length > 0) {
                                    $("tr[area='" + current.AreaId + "']:first").remove();
                                }
                                current.AreaId = item.d.Value;
                                var tbody = "<tr area='" + current.AreaId.toString(10) + "'><td><a href='javascript:Editar({ Nome: \"" + current.Nome + "\", AreaId: " + current.AreaId.toString(10) + " }); void(0);'>" + current.Nome.toUpperCase() +
                                "</a></td><td><div class='btn-group' data-toggle='buttons-radio'><input type='button' id='btnItens" + current.AreaId.toString(10) +
                                "' onclick='GetItensArea(" + current.AreaId.toString(10) + ", this)' class='btn btn-primary' value='itens'/><input type='button' id='btnRemoveArea'" +
                                current.AreaId.toString(10) + " onclick='RemoveArea(" + current.AreaId.toString(10) + ", this)' class='btn btn-danger' value='remover'/></div></td></tr>";

                                $('table[id$="gridArea"] tr:last').after(tbody);
                                $("input[id$='Nome']").val("");
                                $("input[id$='AreaId']").val("");
                            });
                        }
                        this.Excluir = function (item) {
                            var json = JSON.stringify(this);
                            var current = this;
                            __post("Views/Administrativo/AreaConhecimento.aspx/RemoveArea", "{ area :" + json + " }", function (item) {
                                if ($("tr[area='" + current.AreaId + "']").length > 0) {
                                    $("tr[area='" + current.AreaId + "']:first").remove();
                                }
                                current.AreaId = item.d.Value;
                                $("input[id$='Nome']").val("");
                                $("input[id$='AreaId']").val("");
                            });

                        }
                        var __post = function (url, parans, success, error) {
                            var alerta = new Alert();
                            alerta.Type = AlertType.Error;
                            alerta.Name = "erroGeral";
                            $.ajax({
                                url: eS.UrlPadrao + url,
                                type: "post",
                                dataType: "json",
                                contentType: "application/json; charset=utf-8",
                                data: parans,
                                success: function (e) {
                                    if (e.d && e.d.AlertType != undefined) {
                                        alerta.Type = e.d.AlertType;
                                        for (var i in e.d.Messages) {
                                            alerta.AddMessage(e.d.Messages[i]);
                                        }
                                        if (e.d.Show && e.d.Show == true) {
                                            alerta.Show();
                                        }
                                    } else {
                                        if (e.responseText) {
                                            alerta.Type = 2;
                                            alerta.AddMessage(e.responseText);
                                        } else {
                                            alerta.Type = 1;
                                            alerta.AddMessage("Retorno inválido, o objeto retornado não está em um formato correto")
                                        }
                                        alerta.Show();
                                    }
                                    if (alerta.Type == AlertType.Success && success && typeof success == "function") {
                                        success.call(this, e);
                                    }
                                },
                                error: function (e) {
                                    alerta.Type = 1;
                                    alerta.AddMessage(e.responseText);
                                    alerta.Show();
                                    if (error && typeof error == "function") {
                                        error.call(this, e);
                                    }
                                }
                            });
                        }
                    }

                    function RemoveArea(id, link) {
                        var area = new Area();
                        area.AreaId = id;
                        area.Excluir(link);
                    }
                    function Editar(item) {
                        $("input[id$='AreaId']").val(item.AreaId.toString(10));
                        $("input[id$='Nome']").val(item.Nome);
                        return false;
                    }
                    function GetItensArea(area, item) {
                        var url = '<%=this.ResolveUrl("~/Views/Administrativo/ItemArea.aspx?area=")%>';
                        window.location.href = url + area.toString(10);
                    }
                </script>
            </div>
        </div>
    </div>
</asp:Content>
